﻿using Microsoft.Win32;
using NaireSystem.Api;
using NaireSystem.Api.VO;
using NaireSystem.Datas;
using NaireSystem.DBEntity;
using NaireSystem.Enums;
using NaireSystem.ViewModels;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using TYBase;
using TYControls;

namespace NaireSystem.Views
{

    public partial class ViewLearnEditQt : TYWindow
    {
        ViewModelLearnEditFile vm = new ViewModelLearnEditFile();
        private readonly LearnFileVO data;
        private bool newFile = false;


        public ViewLearnEditQt(LearnFileVO data)
        {
            InitializeComponent();
            this.DataContext = vm;
            this.data = data;
            this.Loaded += ViewEditQt_Loaded;
        }

        private async void ViewEditQt_Loaded(object sender, RoutedEventArgs e)
        {
            if (data != null)
            {
                vm.FileModel = UIDataHelper.DataAsUI(data);
                this.Title = "新增问卷题目";
            }
            else
            {
                vm.FileModel = new LearnFileModel(null);
                this.Title = "编辑问卷题目";
            }
            await InitDictData();
        }


        private async void BtnClickOK(object sender, RoutedEventArgs e)
        {
            if (vm.SelectTag == null)
            {
                this.ShowMsg("请选择标签");
                return;
            }

            Tbl_LearnFile tblData = data;
            bool isEdit = data != null;
            int file_id = 0;
            string filePath = vm.FilePath;

            if (data != null)
            {
                file_id = data.file_id;
            }
            if (data == null)
            {
                if (!File.Exists(filePath))
                {
                    this.ShowMsg("无文件!");
                    return;
                }
                tblData = new Tbl_LearnFile();
            }
            else
            {
                if (newFile)//这个时候就需要编辑了
                {
                    if (!File.Exists(filePath))
                    {
                        this.ShowMsg("无文件!");
                        return;
                    }
                }
            }
            tblData.title = vm.FileModel.Title;
            tblData.file_tag = vm.SelectTag.Value;//设置标签 

            var ret = await Run(() =>
            {
                if (isEdit)
                {
                    if (newFile)
                    {

                        return LearnFileApi.EditFile(filePath, tblData);
                    }
                    else
                    {
                        return LearnFileApi.EditFile(tblData);
                    }

                }
                else
                {
                    return LearnFileApi.AddFile(filePath, tblData);
                }
            });
            if (ret.code != 0)
            {
                this.ShowMsg(ret.msg);
                return;
            }
            this.DialogResult = true;
        }





        private void BtnClick_Check(object sender, RoutedEventArgs e)
        {

        }

        private async void BtnClickAddTag(object sender, RoutedEventArgs e)
        {
            ViewEditString view = new ViewEditString();
            if (view.ShowDialog() != true)
                return;
            string data = view.Data;
            {
                var ret = await Run(() =>
             {
                 return DictDataApi.AddItem(new Tbl_DictData
                 {
                     dict_type = "naire_tag_type",
                     dict_label = data
                 });
             });
                if (ret.code != 0)
                {
                    this.ShowMsg(ret.msg);
                    return;
                }
            }

            {
                await InitDictData();
            }

            //private void BtnClickDeleteTag(object sender, RoutedEventArgs e)
            //{
            //    var dx = (sender as Button).DataContext as TagModel;
            //    vm.FileModel.ListTag.Remove(dx);
            //}

            //private void BtnClickAddTag(object sender, RoutedEventArgs e)
            //{
            //    ViewEditString viewEdit = new ViewEditString();
            //    if (viewEdit.ShowDialog() == true)
            //    {
            //        string newData = viewEdit.Data;
            //        if (vm.FileModel.ListTag.Count(x => x.Name == newData) > 0)
            //        {
            //            this.ShowMsg($"标签({newData})已存在!");
            //            return;
            //        }
            //        vm.FileModel.ListTag.Add(new TagModel { Name = newData });
            //    }
            //}

            //private void BtnClick_Check(object sender, RoutedEventArgs e)
            //{
            //    var dx = (sender as CheckBox).DataContext as OptionModel;
            //    if (dx == null)
            //        return;
            //    if (vm.SelectQtType.Key == (int)EnumQtType.SingleSelect
            //        || vm.SelectQtType.Key == (int)EnumQtType.TrueFalse
            //        )
            //    {
            //        foreach (var item in vm.FileModel.ListOption)
            //        {
            //            item.IsSelected = false;
            //        }
            //        dx.IsSelected = true;
            //    }
            //}

            //private void BtnClickMYD(object sender, RoutedEventArgs e)
            //{
            //    if (vm.IsMYDChecked)
            //    {
            //        vm.FileModel.ListOption.Clear();
            //        vm.FileModel.ListOption.Add(new OptionItemModel { Title = "非常满意" });
            //        vm.FileModel.ListOption.Add(new OptionItemModel { Title = "满意" });
            //        vm.FileModel.ListOption.Add(new OptionItemModel { Title = "一般" });
            //        vm.FileModel.ListOption.Add(new OptionItemModel { Title = "不满意" });
            //        if (vm.FileModel.ListTag.Count(x => x.Name == "满意度调查") == 0)
            //        {
            //            vm.FileModel.ListTag.Add(new TagModel { Name = "满意度调查" });
            //        }
            //    }

            //}
        }

        private async Task InitDictData()
        {
            var ret = await Run(() =>
            {
                return DictDataApi.GetList(new Tbl_DictData
                {
                    dict_type = $"{EnumDictType.naire_tag_type}"
                });
            });
            if (ret.code != 0)
            {
                this.ShowMsg(ret.msg);
                return;
            }
            vm.ListTag.Clear();
            foreach (var item in ret.data)
            {
                IntStringData model = new IntStringData { Key = item.dict_value, Value = item.dict_label };
                vm.ListTag.Add(model);
                if (data != null)
                    if (item.dict_label == data.file_tag)
                    {
                        vm.SelectTag = model;
                    }
            }
        }

        private void BtnClick_SelectFile(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = true;
            openFileDialog.Filter = "All files (*.*)|*.*";
            if (openFileDialog.ShowDialog() != true)
            {
                return;
            }
            vm.FilePath = openFileDialog.FileName;
            vm.FileModel.FileName = Path.GetFileName(openFileDialog.FileName);
            newFile = true;

        }
    }
}
